package com.scheduled;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.type.ApplyState;
import com.mrobuy.jdvc.dao.JDAccountDao;
import com.mrobuy.jdvc.dao.VCProductApplyDao;
import com.mrobuy.jdvc.entity.*;
import com.mrobuy.util.IdentifierGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 *  商品申请列表同步
 *
 */
@Component
public class ProductApplyStateSyncTask {

    private static Logger LOG = LoggerFactory.getLogger(ProductApplyStateSyncTask.class);

    @Autowired
    private JDAccountDao jdAccountDao;

    @Autowired
    private VCProductApplyDao vcProductApplyDao;

    @Scheduled(fixedRate = 1000*60*30)
    public void exec() {

        if ( !TaskSwitch.APPLY_STATE_SYNC.get() ) {
            return;
        }

        List<JDAccount> accounts = jdAccountDao.list();
        if ( accounts==null || accounts.size()==0 ) {
            return;
        }

        for (JDAccount account : accounts) {
            boolean sync = "Y".equalsIgnoreCase(account.getAutoSync());
            if ( sync ) {
                stateSync(account);
            }
        }
    }

    private void stateSync(JDAccount account) {

        if ( account==null ) {
            return;
        }

        JdClient client = new DefaultJdClient(Jos.SERVER, account.getAccess_token(),  account.getAppkey(), account.getAppsecret());

        List<VCProductApply> applies = vcProductApplyDao.loadAppliesWithoutComplete(account.getVccode());
        for (VCProductApply apply : applies) {
            LOG.info(apply.getName());

            try {
                if ( apply.getReject()==null
                        || !apply.getReject()
                        ||  StringUtils.isBlank(apply.getOpinion()) ) {
                    VCAuditInfo auditInfo = Jos.productApplyAuditGetClient(client)
                            .setApplyId(apply.getApplyId())
                            .call();
                    if ( auditInfo!=null && StringUtils.isNotBlank(auditInfo.getApplyId())) {
                        apply.setState(auditInfo.getState());
                        apply.setApprovetime(auditInfo.getApprovetime());

                        ApplyState applyState = ApplyState.getStateByCode(auditInfo.getState());
                        apply.setReject(applyState.reject());

                        VCAuditInfo exist = SQLExecutor.executeQueryBean("select * from VC_AUDIT_INFO where APPLYID = ? ", VCAuditInfo.class, auditInfo.getApplyId());
                        if ( exist==null ) {
                            SQLExecutor.insert(auditInfo);
                        } else {
                            SQLExecutor.update(auditInfo);
                        }

                        for (VCAuditRecord auditRecord : auditInfo.getAuditRecords()) {
                            VCAuditRecord existRecord = SQLExecutor.executeQueryBean("select * from VC_AUDIT_RECORD where APPLYID = ? and STATE = ? ", VCAuditRecord.class, auditInfo.getApplyId(), auditRecord.getState());
                            if ( existRecord==null ) {
                                auditRecord.setId(IdentifierGen.uuid());
                                SQLExecutor.insert(auditRecord);
                            }
                        }
                        apply.setOpinion(auditInfo.getOpinion());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOG.error(e.getMessage(), e.getCause());
            }

            vcProductApplyDao.update(apply);
        }
    }

}
